<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="期刊名称" prop="magazineName">
        <el-input
          v-model="queryParams.magazineName"
          placeholder="请输入期刊名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否销售" prop="slae">
        <el-select v-model="queryParams.slae" placeholder="请选择是否销售" clearable>
          <el-option label="销售中" value="1"/>
          <el-option label="未销售" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="queryParams.dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['magazine:magazine:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['magazine:magazine:edit']"
        >修改
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="magazineList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="期刊名称" align="center" prop="magazineName"/>
      <el-table-column label="期刊封面" align="center" prop="magazineCover">
        <template slot-scope="scope">
          <image-preview :src="scope.row.magazineCover" :height="100"/>
        </template>
      </el-table-column>
      <el-table-column label="价格(元)" align="center" prop="price"/>
      <el-table-column label="是否销售" align="center" prop="slae">
        <template slot-scope="scope">
          {{ scope.row.slae == 1 ? '销售中' : '已下架' }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.slae!=1"
                     size="mini"
                     type="text"
                     icon="el-icon-s-goods"
                     @click="uploadGoods(scope.row,1,'上架')"
                     v-hasPermi="['magazine:magazine:edit']"
          >上架
          </el-button>
          <el-button v-else
                     size="mini"
                     type="text"
                     icon="el-icon-s-goods"
                     @click="uploadGoods(scope.row,2,'下架')"
                     v-hasPermi="['magazine:magazine:edit']"
          >下架
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['magazine:magazine:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['magazine:magazine:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改杂志管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="期刊名称" prop="magazineName">
          <el-input v-model="form.magazineName" placeholder="请输入期刊订期名称" style="width: 300px"/>
        </el-form-item>
        <el-form-item label="杂志封面" prop="magazineCover">
          <image-upload v-model="form.magazineCover" :limit="1"/>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" style="width: 300px"/>
        </el-form-item>
        <el-form-item label="期刊备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入期刊备注" style="width: 300px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMagazine, getMagazine, delMagazine, addMagazine, updateMagazine } from '@/api/magazine/magazine'

export default {
  name: 'Magazine',
  data() {
    var checkPrice = function(rule, val, callback) {
      if (!val) {
        return callback(new Error('单价不能为空'))
      }
      var reg = /^\d+(\.{0,1}\d{1,4}){0,1}$/
      if (!reg.test(val)) {
        return callback(new Error('格式错误'))
      }
      return callback()
    }
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 杂志管理表格数据
      magazineList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // $comment时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        magazineName: null,
        slae: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        magazineName: [
          { required: true, message: '期刊名称不能为空', trigger: 'change' }
        ],
        magazineCover: [
          { required: true, message: '期刊封面不能为空', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '价格不能为空 ', trigger: 'blur' },
          { validator: checkPrice, trigger: 'blur' }
        ]

      }
    }

  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询杂志管理列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params['beginCreateTime'] = this.daterangeCreateTime[0]
        this.queryParams.params['endCreateTime'] = this.daterangeCreateTime[1]
      }
      listMagazine(this.queryParams).then(response => {
        this.magazineList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        magazineName: null,
        magazineCover: null,
        price: null,
        slae: null,
        createTime: null,
        updateTime: null,
        remarks: null,
        delFlag: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加期刊'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getMagazine(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改期刊'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMagazine(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addMagazine(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除数据项？').then(function() {
        return delMagazine(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('magazine/magazine/export', {
        ...this.queryParams
      }, `magazine_${new Date().getTime()}.xlsx`)
    },
    uploadGoods(row, val, title) {
      updateMagazine({ id: row.id, slae: val }).then(response => {
        this.$modal.msgSuccess(`${title}成功`)
        this.open = false
        this.getList()
      })
    }
  }
}
</script>
